home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / hotshot / log.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-29  |  6KB  |  196 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import _hotshot
  5. import os.path as os
  6. import parser
  7. import symbol
  8. import sys
  9. from _hotshot import WHAT_ENTER, WHAT_EXIT, WHAT_LINENO, WHAT_DEFINE_FILE, WHAT_DEFINE_FUNC, WHAT_ADD_INFO
  10. __all__ = [
  11.     'LogReader',
  12.     'ENTER',
  13.     'EXIT',
  14.     'LINE']
  15. ENTER = WHAT_ENTER
  16. EXIT = WHAT_EXIT
  17. LINE = WHAT_LINENO
  18.  
  19. class LogReader:
  20.     
  21.     def __init__(self, logfn):
  22.         self._filemap = { }
  23.         self._funcmap = { }
  24.         self._reader = _hotshot.logreader(logfn)
  25.         self._nextitem = self._reader.next
  26.         self._info = self._reader.info
  27.         if self._info.has_key('current-directory'):
  28.             self.cwd = self._info['current-directory']
  29.         else:
  30.             self.cwd = None
  31.         self._stack = []
  32.         self._append = self._stack.append
  33.         self._pop = self._stack.pop
  34.  
  35.     
  36.     def close(self):
  37.         self._reader.close()
  38.  
  39.     
  40.     def fileno(self):
  41.         """Return the file descriptor of the log reader's log file."""
  42.         return self._reader.fileno()
  43.  
  44.     
  45.     def addinfo(self, key, value):
  46.         '''This method is called for each additional ADD_INFO record.
  47.  
  48.         This can be overridden by applications that want to receive
  49.         these events.  The default implementation does not need to be
  50.         called by alternate implementations.
  51.  
  52.         The initial set of ADD_INFO records do not pass through this
  53.         mechanism; this is only needed to receive notification when
  54.         new values are added.  Subclasses can inspect self._info after
  55.         calling LogReader.__init__().
  56.         '''
  57.         pass
  58.  
  59.     
  60.     def get_filename(self, fileno):
  61.         
  62.         try:
  63.             return self._filemap[fileno]
  64.         except KeyError:
  65.             raise ValueError, 'unknown fileno'
  66.  
  67.  
  68.     
  69.     def get_filenames(self):
  70.         return self._filemap.values()
  71.  
  72.     
  73.     def get_fileno(self, filename):
  74.         filename = os.path.normcase(os.path.normpath(filename))
  75.         for fileno, name in self._filemap.items():
  76.             if name == filename:
  77.                 return fileno
  78.                 continue
  79.         
  80.         raise ValueError, 'unknown filename'
  81.  
  82.     
  83.     def get_funcname(self, fileno, lineno):
  84.         
  85.         try:
  86.             return self._funcmap[(fileno, lineno)]
  87.         except KeyError:
  88.             raise ValueError, 'unknown function location'
  89.  
  90.  
  91.     
  92.     def next(self, index = 0):
  93.         while None:
  94.             (what, tdelta, fileno, lineno) = self._nextitem()
  95.             if what == WHAT_ENTER:
  96.                 (filename, funcname) = self._decode_location(fileno, lineno)
  97.                 t = (filename, lineno, funcname)
  98.                 self._append(t)
  99.                 return (what, t, tdelta)
  100.             
  101.             if what == WHAT_EXIT:
  102.                 return (what, self._pop(), tdelta)
  103.             
  104.             if what == WHAT_LINENO:
  105.                 (filename, firstlineno, funcname) = self._stack[-1]
  106.                 return (what, (filename, lineno, funcname), tdelta)
  107.             
  108.             if what == WHAT_DEFINE_FILE:
  109.                 filename = os.path.normcase(os.path.normpath(tdelta))
  110.                 self._filemap[fileno] = filename
  111.                 continue
  112.             if what == WHAT_DEFINE_FUNC:
  113.                 filename = self._filemap[fileno]
  114.                 self._funcmap[(fileno, lineno)] = (filename, tdelta)
  115.                 continue
  116.             if what == WHAT_ADD_INFO:
  117.                 if tdelta == 'current-directory':
  118.                     self.cwd = lineno
  119.                 
  120.                 self.addinfo(tdelta, lineno)
  121.                 continue
  122.             raise ValueError, 'unknown event type'
  123.             continue
  124.             return None
  125.  
  126.     
  127.     def __iter__(self):
  128.         return self
  129.  
  130.     
  131.     def _decode_location(self, fileno, lineno):
  132.         
  133.         try:
  134.             return self._funcmap[(fileno, lineno)]
  135.         except KeyError:
  136.             if self._loadfile(fileno):
  137.                 filename = None
  138.                 funcname = None
  139.             
  140.             
  141.             try:
  142.                 (filename, funcname) = self._funcmap[(fileno, lineno)]
  143.             except KeyError:
  144.                 filename = self._filemap.get(fileno)
  145.                 funcname = None
  146.                 self._funcmap[(fileno, lineno)] = (filename, funcname)
  147.             except:
  148.                 None<EXCEPTION MATCH>KeyError
  149.             
  150.  
  151.             None<EXCEPTION MATCH>KeyError
  152.  
  153.         return (filename, funcname)
  154.  
  155.     
  156.     def _loadfile(self, fileno):
  157.         
  158.         try:
  159.             filename = self._filemap[fileno]
  160.         except KeyError:
  161.             print 'Could not identify fileId', fileno
  162.             return 1
  163.  
  164.         if filename is None:
  165.             return 1
  166.         
  167.         absname = os.path.normcase(os.path.join(self.cwd, filename))
  168.         
  169.         try:
  170.             fp = open(absname)
  171.         except IOError:
  172.             return None
  173.  
  174.         st = parser.suite(fp.read())
  175.         fp.close()
  176.         funcdef = symbol.funcdef
  177.         lambdef = symbol.lambdef
  178.         stack = [
  179.             st.totuple(1)]
  180.         while stack:
  181.             tree = stack.pop()
  182.             
  183.             try:
  184.                 sym = tree[0]
  185.             except (IndexError, TypeError):
  186.                 continue
  187.  
  188.             if sym == funcdef:
  189.                 self._funcmap[(fileno, tree[2][2])] = (filename, tree[2][1])
  190.             elif sym == lambdef:
  191.                 self._funcmap[(fileno, tree[1][2])] = (filename, '<lambda>')
  192.             
  193.             stack.extend(list(tree[1:]))
  194.  
  195.  
  196.